package net.loststone

class IniciController {

    public static final String ERROR_KEY = "error_msg";

    def index = {
        def selection = params.selection;
        if (!selection && params.id) {
            selection = Town.findByGov_id(params.id);
        }

        if (!selection) { 
            forward(action: "search", params: [ERROR_KEY : "No city matched your request"]);
        }

        def level1 = params.country_id;
        def level2 = params.state_id;
        def level3 = params.city_id;
        def level4 = params.town_id;

        return [town : selection];
    }

    def search = {
        if (!params.city_name) {
            params.prev_search = null;
            params.similar = null;
            return params;
        }

        def searchName = params.city_name;
        log.debug("Looking for city ${searchName}");

        def result = Town.findByNameIlike(searchName);
        if (result) {
            params.id = result?.gov_id;
            params.selection = result;
            forward(action: "index", params: params)
        }
        else {
            def similars = Town.findAllByNameIlike("${searchName}%");
            def map = [prev_search : searchName, similars : similars];
            if (similars.size() == 0) {
                map.put(ERROR_KEY, "No city matched your request ${searchName}");
            }
            return map;
        }
    }

    //        Town ejpana = Town.findByGov_id(0)
    //        def indicadorEconomic = EconInfo.findAllByAfecta(ejpana)
    //
    //        // llista de ccaa
    //        def level1 = Town.findAllByParent(ejpana)
    //
    //        // llista provincies + indicador economic de la ccaa.
    //        def level2 = null;
    //        Town selected_state = null;
    //        if (params.state_id) {
    //            indicadorEconomic = null;
    //            selected_state = Town.findByIdAndType(params.state_id, "state")
    //            level2 = Town.findAllByParent(selected_state)
    //            def nowIndicadorEconomic = EconInfo.findAllByAfecta(selected_state)
    //            if (nowIndicadorEconomic) {
    //                indicadorEconomic = nowIndicadorEconomic;
    //            }
    //        }
    //
    //        // agafem llista de pobles + indicador economic de la provincia.
    //        def level3 = null;
    //        Town selected_province = null;
    //        if (params.province_id) {
    //            // agafa la ccaa pare.
    //            indicadorEconomic = null;
    //
    //            selected_province = Town.findByIdAndType(params.province_id, "province")
    //            level2 = Town.findAllByParent(selected_province.parent)
    //            level3 = Town.findAllByParent(selected_province)
    //            def nowIndicadorEconomic = EconInfo.findAllByAfecta(selected_province)
    //            if (nowIndicadorEconomic) {
    //                indicadorEconomic = nowIndicadorEconomic;
    //            }
    //
    //        }
    //
    //        // agafem les dades de la ciutat.
    //        Town selected_town = null;
    //        if (params.town_id) {
    //            indicadorEconomic = null;
    //
    //            selected_town = Town.findByIdAndType(params.town_id, "town")
    //            level3 = Town.findAllByParent(selected_town.parent)
    //            level2 = Town.findAllByParent(selected_town.parent.parent)
    //            def nowIndicadorEconomic = EconInfo.findAllByAfecta(selected_town)
    //            if (nowIndicadorEconomic) {
    //                indicadorEconomic = nowIndicadorEconomic;
    //            }
    //        }
    //
    //        [states: level1, provinces: level2, towns: level3, indicadors_economics : indicadorEconomic,
    //            state : selected_state, province : selected_province, town : selected_town]
    // }

}